package towers;

import java.util.Observable;
import java.util.Set;

import buffs.Buff;

import runner.Enemy;

import main.Coord;
import main.Tickable;

public class Projectile extends Observable implements Tickable {
	private Coord pos;
	private Enemy target;
	private int power;
	private int speed=10;
	private boolean dead=false;
	private Set<Buff> buffs;

	public Projectile(Coord start, Enemy e, int power, Set<Buff> buffs) {
		pos = start;
		target = e;
		this.power = power;
		this.buffs = buffs;
	}

	@Override
	public void tick() {
		if (dead) return;
		Coord t = target.getPos();
		
		if (pos.distanceTo(t) <= speed) {
			target.attack(power);
			if (buffs != null)
				target.addBuffs(buffs);
			dead=true;
			this.setChanged();
			this.notifyObservers();
			return;
		}
		
		pos.x = pos.x+ (int) (Math.cos(pos.angle(t))*speed);
		pos.y = pos.y+ (int) (Math.sin(pos.angle(t))*speed);
	}

	public Coord getPos() {
		return pos;
	}

	public boolean isDead() {
		return dead;
	}
}
